/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.NetworkDao;
import com.tesam.gedeon.entity.Network;
import com.tesam.gedeon.service.NetworkService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Sistema
 */
@Service
public class NetworkServiceImpl implements NetworkService {
    
    @Autowired
    private NetworkDao networkDao ;
    
    public List<Network> getAllByLimit(String dataToSort ,String directionOfOrder , int startLimit , int endLimit){
        return networkDao.getAllByLimit(dataToSort, directionOfOrder, startLimit, endLimit);
    }

    @Override
    public List<Network> getListAll() {
        return networkDao.getListAll();
    }

    @Override
    public int getCountAll() {
        return networkDao.getCountAll();
    }
    
    @Override
    public List<Network> getSearchNetworkByLimit(String dataToSort, String directionOfOrder, int startLimit, int endLimit, Network network){
        return networkDao.getSearchNetworkByLimit(dataToSort, directionOfOrder, startLimit, endLimit, network);
    }
    
    @Override
    public int getTotalBySearch(Network network){
        return networkDao.getTotalBySearch(network);
    }
    
    @Override
    public String getJsonDataFromArrayList(List<Network> listaDeRoles, int cantidadRegistros){
        JSONArray array = null;
        JSONObject json = null;
        String deleteButton = "" ;
        String updateButton = "" ;
        String viewButton = "" ;
        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listaDeRoles.size() > 0) {


                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");

                for (Network network : listaDeRoles) {
                    JSONArray arrayPut = new JSONArray();

                    arrayPut.put(network.getCode()); 
                    arrayPut.put(StringEscapeUtils.escapeHtml4(network.getDescription()));
                    
                    viewButton = "<a href='view?id="+network.getId()+"&module=16' ";
                    viewButton+= "class='btn mini blue'><i class='icon-share'></i> View </a>&nbsp;";
                    
                    updateButton = "<a href='update?id="+network.getId()+"&module=16' class='btn mini green' >" ;
                    updateButton+= "<i class='icon-pencil'></i> Update</a>&nbsp;";
                    
                    deleteButton = "<a class='btn mini red' data-toggle='modal' ";
                    deleteButton+= "onclick=\"";     
                    deleteButton+="confirmDelete('"+network.getId()+"')";
                    deleteButton+="\" "; 
                    deleteButton+="href='#static' >";
                    deleteButton+="<i class='icon-trash'></i> Delete</a>";      
                        
                     //+deleteButton
                    arrayPut.put(viewButton+updateButton+deleteButton);
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", cantidadRegistros);
            json.put("iTotalDisplayRecords", cantidadRegistros);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
    }
    
    @Override
    public List<Network> getNetworkById(Network network){
        return networkDao.getNetworkById(network);
    }
    
    @Override
    public List<Network> updateNetwork(Network network){
        return networkDao.updateNetwork(network);
    }
    
    @Override
    public void deleteNetwork(Network network){
        networkDao.deleteNetwork(network);
    }
    
    @Override
    public int saveNetwork(Network network){
         return networkDao.saveNetwork(network);
    }

}

